<template>
  <div :class="{'w-200':!isCollapse}">
    <el-menu
            router
            :default-active="activeRouter"
            :default-openeds="defaultOpened"
            :collapse="isCollapse">
      <template v-for="item in menus">
        <el-menu-item v-if="item.menuDtos === undefined" :index="item.url" :key="item.url">
          <i :class="['iconfont',item.icon]"></i>
          <span slot="title">{{item.name}}</span>
        </el-menu-item>
        <el-submenu v-else :index="item.url" :key="item.url">
          <template slot="title">
            <i :class="['iconfont',item.icon]"></i>
            <span slot="title">{{item.name}}</span>
          </template>
          <el-menu-item :index="item.url" v-for="(item,idx) in item.menuDtos" :key="item.url">
            <template slot="title">
              <i :class="['iconfont','icon-daohangweixuan']"></i>
              <span slot="title">{{item.name}}<v-plain-tips :num="getTips(item)"/></span>
            </template>
          </el-menu-item>

        </el-submenu>
      </template>
      <div class="ctrl" @click="toggleCollapse"><i class="iconfont icon-qiehuan"></i></div>
    </el-menu>
  </div>
</template>

<script lang="ts">
  import { Vue, Component, Prop } from 'vue-property-decorator'
  import vPlainTips from '@/components/plain-tips'
  import { Getter } from 'vuex-class'

  @Component({
    components: {
      vPlainTips
    }
  })
  export default class Menu extends Vue {
    @Prop({
      default: () => []
    }) menus
    isCollapse: boolean = false

    get defaultOpened (): string[] {
      return this.menus.map(v => v.url)
    }

    get activeRouter (): string {
      return this.$route.meta.activeRoot
    }

    toggleCollapse (): void {
      this.isCollapse = !this.isCollapse
      window.localStorage.setItem('isCollapse', this.isCollapse ? 't' : '')
    }

    getTips (item) {
      switch (item.url) {
        case '/transaction/inventory':
          return this.menuTips.countBill
        case '/transaction/order':
          return this.menuTips.orderCount
        case '/transaction/offer':
          return this.menuTips.inquiryCount
      }
    }

    @Getter menuTips

    mounted () {
      let c = window.localStorage.getItem('isCollapse')
      this.isCollapse = !!c
    }
  }
</script>

<style lang="less" scoped>
  .w-200 {
    width: 200px;
  }

  .el-menu {
    border: none;
    border-radius: 10px;
  }

  .iconfont {
    margin-right: 10px;
    font-size: 20px;
  }

  .icon-daohangweixuan {
    font-size: 12px;
    transform: scale(0.5);
  }

  .ctrl {
    text-align: center;
    height: 56px;
    line-height: 56px;
    cursor: pointer;
    &:hover {
      background: #ecf5ff;
    }
    > i {
      margin: 0;
    }
  }
</style>
